package com.xjh.mange.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.mange.common.constant.UserSessionName;
import com.xjh.mange.common.enums.FinishStatusEnum;
import com.xjh.mange.common.util.SessionUtil;
import com.xjh.mange.model.Thing;
import com.xjh.mange.model.ThingFinishTime;
import com.xjh.mange.model.User;
import com.xjh.mange.service.LogService;
import com.xjh.mange.service.ThingFinishTimeService;
import com.xjh.mange.service.ThingService;
import com.xjh.mange.vo.LayuiTableVo;
import com.xjh.mange.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 徐佳辉
 * @date 2020/12/27 19:56
 */
@Controller
@RequestMapping("/thing")
public class ThingController {
    @Autowired
    private ThingService thingService;
    @Autowired
    private ThingFinishTimeService thingFinishTimeService;
    @Autowired
    private LogService logService;

    /**
     * 分页
     *
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public LayuiTableVo list(int page, int limit) {
        IPage<Thing> page1 = new Page<>(page, limit);
        QueryWrapper<Thing> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        thingService.page(page1, wrapper);
        return LayuiTableVo.ok(page1.getRecords(), (int) page1.getTotal());
    }

    /**
     * 添加每日的事
     *
     * @param thing
     * @param session
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    public ResponseVO insert(@RequestBody Thing thing, HttpSession session) {
        User user = (User) session.getAttribute(UserSessionName.USER);
        if (user == null) {
            return ResponseVO.error("请登录");
        }
        if (thing.getThingId() != null) {
            Thing byId = thingService.getById(thing.getThingId());
            byId.setThing(thing.getThing());
            boolean b = thingService.updateById(byId);
            logService.Log(SessionUtil.getName(session), "更新每日的事:" + byId.getThing());
            return b ? ResponseVO.ok("更新成功") : ResponseVO.error("更新失败");
        } else {
            Thing thing1 = new Thing();
            thing1.setCreateTime(new Date());
            thing1.setThing(thing.getThing());
            thing1.setUserId(user.getUserId());
            thing1.setState(FinishStatusEnum.NOT_FINISH.getCode());
            boolean save = thingService.save(thing1);
            logService.Log(SessionUtil.getName(session), "添加每日的事:" + thing1.getThing());
            return save ? ResponseVO.ok("添加成功") : ResponseVO.error("添加失败");
        }

    }

    /**
     * 改变完成状态
     *
     * @param id
     * @return
     */
    @PostMapping("/change")
    @ResponseBody
    public ResponseVO change(Integer id, HttpSession session) {
        User user = (User) session.getAttribute(UserSessionName.USER);
        if (user == null) {
            return ResponseVO.error("请登录");
        }
        Thing byId = thingService.getById(id);
        if (byId.getState() == FinishStatusEnum.FINISH.getCode()) {
            byId.setState(FinishStatusEnum.NOT_FINISH.getCode());
            // 记录到
            QueryWrapper<ThingFinishTime> wrapper = new QueryWrapper<>();
            wrapper.eq("thing_id", byId.getThingId());
            List<ThingFinishTime> list = thingFinishTimeService.list(wrapper);
            for (ThingFinishTime thingFinishTime : list) {
                if (DateUtil.isSameDay(new Date(), thingFinishTime.getFinishTime())) {
                    QueryWrapper<ThingFinishTime> wrapper1 = new QueryWrapper<>();
                    wrapper1.eq("thing_id", thingFinishTime.getThingId());
                    wrapper1.eq("finish_time", thingFinishTime.getFinishTime());
                    thingFinishTimeService.remove(wrapper1);
                }
            }

        } else {
            byId.setState(FinishStatusEnum.FINISH.getCode());
            // 记录
            ThingFinishTime thingFinishTime = new ThingFinishTime();
            thingFinishTime.setFinishTime(new Date());
            thingFinishTime.setThingId(byId.getThingId());
            thingFinishTime.setUserId(user.getUserId());
            thingFinishTimeService.save(thingFinishTime);
        }
        boolean b = thingService.updateById(byId);
        logService.Log(SessionUtil.getName(session), "改变每日的事状态:" + byId.getThing());
        return b ? ResponseVO.ok("状态改变成功") : ResponseVO.error("状态改变失败");

    }

    /**
     * 更新
     *
     * @param thing
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResponseVO update(Thing thing, HttpSession session) {
        Thing byId = thingService.getById(thing.getThingId());
        byId.setThing(thing.getThing());
        boolean b = thingService.updateById(byId);
        logService.Log(SessionUtil.getName(session), "更新每日的事:" + byId.getThing());
        return b ? ResponseVO.ok("更新成功") : ResponseVO.error("更新失败");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteOne")
    @ResponseBody
    public ResponseVO deleteone(Integer id, HttpSession session) {
        Thing byId = thingService.getById(id);
        boolean b = thingService.removeById(id);
        logService.Log(SessionUtil.getName(session), "删除每日的事:" + byId.getThing());
        QueryWrapper<ThingFinishTime> wrapper = new QueryWrapper<>();
        wrapper.eq("thing_id", id);
        boolean remove = thingFinishTimeService.remove(wrapper);
        return b ? ResponseVO.ok("删除成功") : ResponseVO.error("删除失败");
    }

}
